#!/usr/bin/env bash
# Columnstore Team Use Only
# shellcheck source=/dev/null

source .env

USE_COMPOSE=${USE_COMPOSE:-true}
MAXSCALE=${MAXSCALE:-false}
PM1=${PM1:-mcs1}
PM2=${PM2:-mcs2}
PM3=${PM3:-mcs3}
MCS_IMAGE_NAME=${MCS_IMAGE_NAME:-mariadb/columnstore}
USE_S3_STORAGE=${USE_S3_STORAGE:-false}
S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-myaccesskey}
S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-mysecretkey}
S3_BUCKET=${S3_BUCKET:-mybucket}
S3_ENDPOINT=${S3_ENDPOINT:-s3.amazonaws.com}
S3_REGION=${S3_REGION:-us-west-2}

if [[ "${USE_COMPOSE}" == true ]]; then
    if [[ "${MAXSCALE}" == true ]]; then
        docker compose -f docker-compose-mxs.yml up -d
        docker exec -it "${PM1}" provision "${PM1}" "${PM2}" "${PM3}"
    else
        docker compose -f docker-compose.yml up -d
        docker exec -it "${PM1}" provision "${PM1}" "${PM2}" "${PM3}"
    fi
else
    docker run -d \
        --name "${PM1}" \
        --hostname "${PM1}" \
        --env PM1="${PM1}" \
        --env USE_S3_STORAGE="${USE_S3_STORAGE}" \
        --env S3_ACCESS_KEY_ID="${S3_ACCESS_KEY_ID}" \
        --env S3_SECRET_ACCESS_KEY="${S3_SECRET_ACCESS_KEY}" \
        --env S3_BUCKET="${S3_BUCKET}" \
        --env S3_ENDPOINT="${S3_ENDPOINT}" \
        --env S3_REGION="${S3_REGION}" \
        --env CGROUP="${CGROUP}" \
        -p 127.0.0.1:3307:3306 \
        --shm-size=512mb "${MCS_IMAGE_NAME}"
    docker exec -it "${PM1}" provision "${PM1}"
fi
